package com.ssm.controller;


import com.ssm.pojo.Information;
import com.ssm.pojo.UserInformation;
import com.ssm.service.MessageDelivery;
import com.ssm.service.OperationMessage;
import com.ssm.utils.UserContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class Inchat {
    @Resource(name = "OperationMessageImp")
    private OperationMessage operationMessage;

    @Resource(name = "MessageDeliveryImp")
    private MessageDelivery messageDelivery;

    @RequestMapping(value="/addMes")
    @ResponseBody
    public Map<Object, Object> addMes(Long toId,String text){
        //  state 0,成功，1、失败，2、参数数据为空
        Map<Object, Object> data = new HashMap<>();
        if(toId == null || text == null){
            data.put("state",2);
        }else{
            Information m = new Information();
            Long fromId = ((UserInformation) UserContext.getUser()).getId();
            m.setFromId((long)fromId);
            m.setToId(toId);
            m.setText(text);
            Boolean addt = false;
            try {
                addt = operationMessage.addMes(m);
                if(addt){
                    data.put("state",0);
                    messageDelivery.sendMes(toId,text);
                }else{
                    data.put("aa",1);
                }
            }catch (Exception e){
                data.put("aa",1);
            }
        };
        return data;
    }
}
